package com.ssm.controller;

import com.ssm.pojo.Books;
import com.ssm.service.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Cat.Sir
 * Description:
 * DateTime: 2022-04-29 10:43
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("bookService")
    private BookServiceImpl bookService;

    @RequestMapping("/allBook")
    public String list(Model model) {
        int AllPage = 0;
        int AllRow = 0;
        List<Books> findRow = bookService.queryAllBook();
        for (Books book : findRow) {
            if (book != null) {
                AllRow += 1;
            }
        }
        if (AllRow != 0) {
            if (AllRow % 5 == 0) {
                AllPage = AllRow / 5;
            } else {
                AllPage = AllRow / 5 + 1;
            }
        }
        List<Books> list = bookService.queryBookByPage(0, 5);
        model.addAttribute("list", list);
        model.addAttribute("AllPage", AllPage);
        return "allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdate(@RequestParam int id, Model model) {
        Books book = bookService.queryBookById(id);
        model.addAttribute("book", book);
        return "upDateBook";
    }

    @RequestMapping("/updateBook")
    public String update(Books books) {
        int i = bookService.updateBook(books);
        return "redirect:allBook";
    }

    @RequestMapping("/toAddBook")
    public String toAdd() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(Books books, Model model) {
        int i = bookService.addBook(books);
        System.out.println(books.toString());
        return "redirect:allBook";
    }

    @RequestMapping("/delete")
    public String deleteBook(@RequestParam int id, HttpServletRequest request) {
        String fileName = bookService.queryBookNameById(id);
        String filePath = request.getServletContext().getRealPath("/upload");
        File file = new File(filePath, fileName + ".jpg");
        if (file.exists()) {
            file.delete();
        }
        int i = bookService.deleteBookById(id);
        return "redirect:allBook";
    }

    @RequestMapping("/queryBook")
    public String queryBook(@RequestParam String bookName, Model model) {
        Books books = bookService.queryBookByName(bookName);
        List<Books> list = new ArrayList<>();
        list.add(books);
        model.addAttribute("list", list);
        return "allBook";
    }

    @RequestMapping("/page")
    public String queryBookByPage(int pageNum, Model model, int AllPage) {
        if (pageNum == 1) {
            return "redirect:allBook";
        }
        int start = 5 * (pageNum - 1);
        int pageSize = 5;
        List<Books> list = bookService.queryBookByPage(start, pageSize);
        model.addAttribute("list", list);
        model.addAttribute("AllPage", AllPage);
        return "allBook";
    }
}
